package academic.emailClient.view;


import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class SearchInTheMailsView {
	
	private JPanel searchFrame=new JPanel(new FlowLayout(FlowLayout.CENTER, 3,3));
	private JLabel from=new JLabel("From                      ");
	private JLabel to=new JLabel("To                           ");
	private JLabel subject=new JLabel("Subject                  ");
	private JLabel includeTheWord=new JLabel("Include the world");
	private JLabel date=new JLabel("Date                       ");
	private JLabel empty=new JLabel("                             ");
	private JLabel filterName=new JLabel("Filter Name");
	private JLabel filterPathLabel=new JLabel("Folder to save filter to");
	final static String[] DAYS ={"--", "01","02","03","04","05","06","07","08","09","10","11","12","13", "14","15","16","17", "18","19","20","21", 
			"22","23","24","25", "26", "27", "28", "29", "30", "31"};	
	final static String[] MONTHS ={"--", "January","February","March","April","May","June","July","August","Spetember",
			"October","November","December"};	
	final static String  YEAR[] = {"--", "2013","2014","2015","2016", "2017","2018","2019","2020","2021","2022","2022","2023"};
	private static JTextField fromTxt=new JTextField(25);
	private static JTextField toTxt=new JTextField(25);
	private static JTextField subjectTxt=new JTextField(25);
	private static JTextField includeTheWordTxt=new JTextField(25);
	private JTextField filterNameTxt=new JTextField(25);
	private static JComboBox daysCb= new JComboBox(DAYS);
	private static JComboBox monthsCb= new JComboBox(MONTHS);
	private static JComboBox yearCb= new JComboBox(YEAR);	
	private JComboBox filterPathCombo;
	private JButton search=new JButton("Go");
	private JButton executeFilterBtn =new JButton("Execute Filter");
	private JButton filter=new JButton("Add as a Filter");
	private JButton saveFilter=new JButton("Save Filter");	
	private ArrayList<String> values;
	private HashMap<String, String> paths;	
	ImageIcon aIcon =  createImageIcon("icons/expand_icon.gif");
  
	/**
	 * Create the Search Panel
	 * @return
	 * @throws IOException
	 */
	
    public JPanel create() throws IOException{
    	filter.setIcon(aIcon);
    	searchFrame.setPreferredSize(new Dimension(400, 200));
    	searchFrame.setLayout(new FlowLayout(FlowLayout.LEFT, 3,3));
		searchFrame.add(from);
		searchFrame.add(fromTxt);
		searchFrame.add(to);
		searchFrame.add(toTxt);
		searchFrame.add(subject);
		searchFrame.add(subjectTxt);	
		searchFrame.add(includeTheWord);
		searchFrame.add(includeTheWordTxt);		
		searchFrame.add(date);
		searchFrame.add(daysCb);
		searchFrame.add(monthsCb);
		searchFrame.add(yearCb);
		searchFrame.add(empty);
		searchFrame.add(search);
		searchFrame.add(filter);
		
		return searchFrame;	
	}
    
    /**
     * to have the filtering criteria from user
     */
	
	public static  ArrayList<String> takingTheValues(){
		ArrayList<String> values;
		values=new ArrayList<String>();
		values.add(fromTxt.getText());
		values.add(toTxt.getText());
		values.add(subjectTxt.getText());
		values.add(includeTheWordTxt.getText());
		String date = getDate();
		values.add(getDate());
		return values;
	}
	
	/**
	 * get date in this format dd/mm/yyyy
	 * @return
	 */
	public static String getDate(){
		if((daysCb.getSelectedIndex()!= 0) && (daysCb.getSelectedIndex()!= 0) && (yearCb.getSelectedIndex()!= 0)){
			int index = monthsCb.getSelectedIndex();
			String date = "";
			if(index<10)
			{
				date = "0"+Integer.toString(index);
			}
			else
			{
				date = Integer.toString(index);
			}
			
			return 	daysCb.getSelectedItem().toString() + "/" + date + "/" + yearCb.getSelectedItem().toString();
		  }  
		return "";
		}
		
	/**
	 * Get filter Name
	 * @return
	 */
	public String getFilterName(){
		return 	filterNameTxt.getText();
		}
		
	/**
	 * getFilterPath
	 * @return
	 */
	public String getFilterPath(){
		String path = paths.get(filterPathCombo.getSelectedItem().toString());
		return path;
	}
/**
 * the method that creates the filter view	
 * @return
 */
	public JPanel createFilterView(HashMap<String, String> paths)
	  {
		JPanel filterPanel=new JPanel();
		filterPanel.setPreferredSize(new Dimension(400, 200));
		filterPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 3,3));
    	filterPanel.add(filterName);
    	filterPanel.add(filterNameTxt);
    	filterPanel.add(filterPathLabel);    	
    	
    	String[] filterPathstring=createMenuItem(paths);
    			 
   	    filterPathCombo = new JComboBox(filterPathstring);
		filterPanel.add(filterPathCombo);
		filterPanel.add(saveFilter);
		filterPanel.add(executeFilterBtn);

		return filterPanel;
	}
	
	/**
	 * create Menu Item
	 * @param paths
	 * @return
	 */
	private String[] createMenuItem(HashMap<String, String> paths){

    	this.paths=paths;

    	Set<String> visiblePathsSet = paths.keySet();
		Iterator<String> iterator = visiblePathsSet.iterator();
		 String[] filterPaths=new String[visiblePathsSet.size()];
			
		for (int i=0;i< visiblePathsSet.size();i++)
			filterPaths[i]=iterator.next();
		return filterPaths;		
	}
	
	public void addSearchingMessageActionListener(ActionListener listener) {
		search.addActionListener(listener);
	}
	public void addFilterMessageActionListener(ActionListener listener) {
		filter.addActionListener(listener);
	}
	
	public void addTheFilterActionListener(ActionListener listener) {
		saveFilter.addActionListener(listener);
	}
	
	
	public void addExecuteFilterActionListener(ActionListener listener) {
		executeFilterBtn.addActionListener(listener);
	}
	
	/** this method returns an Image Icon. Otherwise, if the path is not valid, null will be returned. */
	private ImageIcon createImageIcon(String path) {
		if (DynamicTree.class.getResource(path) != null) {
			return new ImageIcon(DynamicTree.class.getResource(path));
		} 
		else
		{
			System.err.println("file not found at this location: " + path);
			return null;
		}
	}

}


